<?php
$appid = "wx92e23e5b5cfee7c3";
$appsecret = "1ea79b1c73391dde3b60200a2e7518e8";
$wechat_token_file = __DIR__.'/Wxcatch/wechat_token.txt';
$wechat_wechat_ticket = __DIR__.'/Wxcatch/wechat_ticket.txt';
header("content-Type:application/json;charset=utf-8");
header("Access-Control-Allow-Origin:*");
header("Access-Control-Allow-Credentials:true");
if(file_exists($wechat_token_file)) 
    $token_data = file_get_contents($wechat_token_file); 
$time = 7200;
if (!empty($token_data)) {
    $token_data = json_decode($token_data, true);
    $time  = time() - $token_data['time'];
}
if ($time > 3600) {
    $token_res = curl_request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret"); 
    $token_res = json_decode($token_res, true);
    $token = $token_res['access_token'];
    $data = array(
        'time' =>time(),
        'token' =>$token
    ); 
    $res = file_put_contents($wechat_token_file, json_encode($data)); 
} else {
    $token = $token_data['token'];
}


// 获取ticket
if(file_exists($wechat_wechat_ticket))   
    $ticket_data = file_get_contents($wechat_wechat_ticket);
$time = 7200;
if (!empty($ticket_data)) {
    $ticket_data = json_decode($ticket_data, true);
    $time  = time() - $ticket_data['time'];
}
if ($time > 3600) {
    $ticket_res = curl_request("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={$token}&type=jsapi"); 
    $ticket_res = json_decode($ticket_res, true);
    $ticket = $ticket_res['ticket'];
    $data = array(
        'time'    =>time(),
        'ticket'  =>$ticket
    );
    $res = file_put_contents($wechat_wechat_ticket, json_encode($data));  
} else {
    $ticket = $ticket_data['ticket'];
} 

// 进行sha1签名
$timestamp = time();
$nonceStr =  createNonceStr();
// 注意 URL 建议动态获取(也可以写死).
// $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
// $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; // 调用JSSDK的页面地址
// echo $url;
$url = $_SERVER['HTTP_REFERER']; // 前后端分离的, 获取请求地址(此值不准确时可以通过其他方式解决)
// $url = "http://39.98.134.216:7000"; // 前后端分离的, 获取请求地址(此值不准确时可以通过其他方式解决)
$str = "jsapi_ticket={$ticket}&noncestr={$nonceStr}&timestamp={$timestamp}&url={$url}";
$sha_str = sha1($str);
$signPackage = array(
    "appId" =>  $appid,
    "nonceStr" => $nonceStr,
    "timestamp" => $timestamp,
    "signature" => $sha_str,
    "url" => $url,
    "ticket" => $ticket,
);
echo json_encode($signPackage); 

// //随机字符串
function createNonceStr($length = 16) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }

function curl_request($url,$method='get',$data=null,$https=true){
    //1.初识化curl
    $ch = curl_init($url);
    //2.根据实际请求需求进行参数封装
    //返回数据不直接输出
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    //如果是https请求
    if($https === true){
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
    }
    //如果是post请求
    if($method === 'post'){
        //开启发送post请求选项
        curl_setopt($ch,CURLOPT_POST,true);
        //发送post的数据
        curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
    }
    //3.发送请求
    $result = curl_exec($ch);
    //4.返回返回值，关闭连接
    curl_close($ch);
    return $result;
}
